﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using Model;

public partial class SinglePage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        int uid = int.Parse(Cookies.getCookies("uid"));
        int lastStatus = 0;
        if(Request["ls"]!=null)
        {
            lastStatus = int.Parse(Request["ls"]);
           
        }
        switch (lastStatus)
        {
            case 1:
                Window1.Title = "沒接";
                break;
            case 2:
                Window1.Title = "不要";
                break;
            case 3:
                Window1.Title = "跟進";
                break;
            case 4:
                Window1.Title = "未处理";
                break;
            case 5:
                Window1.Title = "確實";
                break;
            default:
                break;
        }
        WinBind(uid, lastStatus);
        if(!X.IsAjaxRequest)
        {
            CTMLastStatus.SetValue(lastStatus);
            //using (var m = new ACEntities())
            //{
            //    var StoreBigClass = from c in m.Catalogs
            //                        where c.ParentID == 0
            //                        select c;
                 
            //    StoreBC1.DataSource = StoreBigClass;
            //    StoreBC1.DataBind();

            //}
            
        }
        
    }

    protected void StoreSC1_onRefresh(object sender, StoreReadDataEventArgs e)
    {
        int pid = int.Parse(BigClassID.SelectedItem.Value);
        using (var m = new ACEntities())
        {
            var StoreSmallClass = from c in m.Catalogs
                                  where c.ParentID == pid
                                  select c;
            StoreSC1.DataSource = StoreSmallClass;
            StoreSC1.DataBind();
        }
    }

    protected void SaveGenJin(object sender, DirectEventArgs e)
    {
        int ctmid = int.Parse(CTMID.Text);
        int uid = int.Parse(Cookies.getCookies("uid"));
        int status = int.Parse(e.ExtraParams["s"]);
        using (var m = new ACEntities())
        {
            var q = (from c in m.Customers
                     where c.CTMID == ctmid && c.isStatus == true 
                     select c).FirstOrDefault();
            var c1 = new global::Model.Customers();
            c1.CTMID = q.CTMID;
             
            c1.BigClassID = int.Parse(q.BigClassID.ToString());
            c1.SmallClassID = int.Parse(q.SmallClassID.ToString());
            c1.CTMCompany = CTMCompany.Text;
            c1.CTMUsername = CTMUsername.Text;
            c1.CTMPhone = CTMPhone.Text;
            c1.CTMFax = CTMFax.Text;
            c1.CTMEmail = CTMEmail.Text;
            c1.CTMAddress = CTMAddress.Text;
            c1.LastTime = DateTime.Now;
            c1.LastUserID = uid;
            c1.LastStatus = status;
            c1.LastRemarks = CTMLastRemarks.Text;

            if (status!=2&&status!=1)
            {
                c1.NextContectTime = DateTime.Parse(CTMNextContectTime.Value.ToString());
            }

            c1.inTime = DateTime.Now;
            c1.isStatus = true;
            m.AddToCustomers(c1);
            q.isStatus = false;
            m.SaveChanges();
             
    
            
            CTMID.Clear();
            BigClassID.Clear();
            SmallClassID.Clear();
            CTMCompany.Clear();
            CTMUsername.Clear();
            CTMPhone.Clear();
            CTMFax.Clear();
            CTMEmail.Clear();
            CTMAddress.Clear();

            WinBind(uid, int.Parse(Request["ls"]));
        }
    }

    protected void WinBind(int uid,int lastStatus)
    {
        using (var m = new ACEntities())
        {
            var q = (from c in m.Customers
                     where c.LastStatus == lastStatus &&c.LastUserID == uid && c.isStatus == true
                     orderby c.LastTime ascending 
                     select c).FirstOrDefault();
            if (q != null)
            {
                CTMNextContectTime.Value = DateTime.Now;
                CTMID.Text = q.CTMID.ToString();
                //BigClassID.SetValue(q.BigClassID);

                CTMCompany.SetValue(q.CTMCompany.ToString());
                CTMUsername.SetValue(q.CTMUsername);
                CTMPhone.SetValue(q.CTMPhone);
                CTMFax.SetValue(q.CTMFax.ToString());
                CTMEmail.SetValue(q.CTMEmail);
                CTMAddress.SetValue(q.CTMAddress);
            }
            else
            {
                X.Msg.Alert("警告","没有任何数据！").Show();
            }
            //X.AddScript("#{StoreSC1}.reload();");
            //SmallClassID.SetValue(q.SmallClassID);
        }
    }
}